function reduce(func, list, init) {
  var acc = null;
  for (var i = init ? -1 : 0, acc = init ? init : list[0]; i < list.length - 1; i += 1, acc = func(acc, list[i])) {
  };
  __PS_MV_REG = [];
  return acc;
};
function car(ls) {
  return ls[0];
};
function cdr(ls) {
  return ls.slice(1);
};
function cadr(ls) {
  return car(cdr(ls));
}
function cddr(ls) {
  return cdr(cdr(ls));
}
function reverse(ls) {
  return ls.slice().reverse();
};
function take(ls, start, end) {
  return ls.slice().splice(start,end);
};
function zerowhat(n) {
  return 0 === n;
};
function bang(x) {
  var total = 1;
  for (var i = 0; i < x + 1; i += 1) {
    if (!zerowhat(i)) {
      total = i * total;
    };
  };
  __PS_MV_REG = [];
  return total;
};

if ('undefined' === typeof PAGE) {
  var PAGE = 'calc';
};

if ('undefined' === typeof MENU_ITEM) {
  var MENU_ITEM = 0;
};


if ('undefined' === typeof STACK) {
  var STACK = [];
};

if ('undefined' === typeof MENU) {
  var MENU = [
    {
      label: '<span>&#8730;</span>x',
      fn: () => doOperationbang(function (x) {
        return Math.sqrt(x);
      }, 1),
    },
    {
      label: '<var>x<sup>y</sup></var>',
      fn: () => doOperationbang(function (x, y) {
        return Math.pow(x, y);
      }, 2),
    },
    {
      label: 'Round to <var>x</var>',
      fn: () => doOperationbang(function (x, y) {
        return y.toFixed(x);
      }, 2),
    },
    {
      label: 'Round to whole',
      fn: () => doOperationbang(function (x) {
        return Math.round(x);
      }, 1),
    },
    {
      label: 'Ceiling',
      fn: () => doOperationbang(function (x) {
        return Math.ceil(x);
      }, 1),
    },
    {
      label: 'Floor',
      fn: () => doOperationbang(function (x) {
        return Math.floor(x);
      }, 1),
    },
    {
      label: '<span>π</span>',
      fn: () => {
        document.getElementById('operand').innerHTML = Math.PI;
        addToStackbang();
        return renderStackbang();
      },
    },
    {
      label: 'sin(x)',
      fn: () => doOperationbang(function (x) {
        const rads = x * Math.PI / 180;
        return Math.sin(rads);
      }, 1),
    },
    {
      label: 'cos(x)',
      fn: () => doOperationbang(function (x) {
        const rads = x * Math.PI / 180;
        return Math.cos(rads);
      }, 1),
    },
    {
      label: 'tan(x)',
      fn: () => doOperationbang(function (x) {
        const rads = x * Math.PI / 180;
        return Math.tan(rads);
      }, 1),
    },
    {
      label: 'asin(x)',
      fn: () => doOperationbang(function (x) {
        const rads = x * Math.PI / 180;
        return Math.asin(rads);
      }, 1),
    },
    {
      label: 'acos(x)',
      fn: () => doOperationbang(function (x) {
        const rads = x * Math.PI / 180;
        return Math.acos(rads);
      }, 1),
    },
    {
      label: 'atan(x)',
      fn: () => doOperationbang(function (x) {
        const rads = x * Math.PI / 180;
        return Math.atan(rads);
      }, 1),
    },
    {
      label: '<span>e</span>',
      fn: () => {
        document.getElementById('operand').innerHTML = Math.E;
        addToStackbang();
        return renderStackbang();
      },
    },
    {
      label: '<var>e<sup>x</sup></var>',
      fn: () => doOperationbang(function (x) {
        return Math.exp(x);
      }, 1),
    },
    {
      label: '<var>ln(x)</var>',
      fn: () => doOperationbang(function (x) {
        return Math.log(x);
      }, 1),
    },
    {
      label: '<var>log<sub>10</sub></var>',
      fn: () => doOperationbang(function (x) {
        return Math.log10(x);
      }, 1),
    },
  ];
};


function clearOperandbang() {
  var elem = document.getElementById('operand');
  __PS_MV_REG = [];
  return elem.innerHTML = '';
};

function addToStackbang() {
  var elem = document.getElementById('operand');
  var value = elem.innerHTML;
  if (value === '' || value === undefined) {
    console.log(STACK, car(STACK), [car(STACK)], [car(STACK)].concat(STACK));
    STACK = [car(STACK)].concat(STACK);
  } else {
    STACK = [value].concat(STACK);
  };
  __PS_MV_REG = [];
  return clearOperandbang();
};

function renderStackbang() {
  var e = document.getElementById('stack');
  var leftSoftkey = document.getElementById('left');
  var i = STACK.length;
  var html = zerowhat(i) ? '' : reduce(function (a, c) {
    i--
    var register = i
    if(i === 2) {
      register = 'z';
    } else if(i === 1) {
      register = 'y';
    } else if (zerowhat(i)) {
      register = 'x';
    }
    var body = `${register}: <b>${c}</b>`;
    var n = `
<div id="${c}" class="stack-item">
<p class="inner-stack-item">
${body}
</p>
</div>
`;
    return 'start' === a ? n : a + n;
  }, reverse(STACK), 'start');
  leftSoftkey.innerHTML = 'Swap';
  __PS_MV_REG = [];
  return e.innerHTML = html;
};

function doOperationbang(operation, numberRequired) {
  var elem = document.getElementById('operand');
  if (elem.innerHTML === '') {
    if (STACK.length === 0 || STACK.length < numberRequired) {
      alert('Need ' + numberRequired + ' items in the stack!');
    } else {
      var operands = take(STACK, 0, numberRequired);
      var numericalAnswer = operands.length === 1 ? operation(Number(car(operands))) : reduce(function (a, c) {
        var na = Number(a);
        var nc = Number(c);
        if (a && c) {
          __PS_MV_REG = [];
          return operation(na, nc);
        } else if (a) {
          __PS_MV_REG = [];
          return operation(na);
        } else if (c) {
          __PS_MV_REG = [];
          return operation(nc);
        } else if (otherwise) {
          __PS_MV_REG = [];
          return a;
        };
      }, operands);
      var answer = String(numericalAnswer);
      var stackWithout = STACK.slice(numberRequired);
      console.log(stackWithout, STACK, numberRequired);
      STACK = [answer].concat(stackWithout);
    };
  } else {
    if (STACK.length === 0) {
      alert('Need ' + (numberRequired - 1) + ' items in the stack!');
    } else {
      var elem1 = document.getElementById('operand');
      var stack = [elem1.innerHTML].concat(STACK);
      var operands2 = reverse(take(stack, 0, numberRequired));
      var numericalAnswer3 = 1 === operands2.length ? operation(Number(car(operands2))) : reduce(function (a, c) {
        var na = Number(a);
        var nc = Number(c);
        if (a && c) {
          __PS_MV_REG = [];
          return operation(na, nc);
        } else if (a) {
          __PS_MV_REG = [];
          return operation(na);
        } else if (c) {
          __PS_MV_REG = [];
          return operation(nc);
        } else if (otherwise) {
          __PS_MV_REG = [];
          return a;
        };
      }, operands2);
      var answer4 = String(numericalAnswer3);
      var stackWithout5 = STACK.slice(numberRequired - 1);
      STACK = [answer4].concat(stackWithout5);
    };
  };
  renderStackbang();
  __PS_MV_REG = [];
  return clearOperandbang();
};

function clearAllbang() {
  STACK = [];
  __PS_MV_REG = [];
  return renderStackbang();
};

const openMenu = () => {
  PAGE = 'menu';
  MENU_ITEM = 0;
  document.getElementById("left").innerHTML = "";
  document.getElementById("right").innerHTML = "Back";
  document.getElementById("middle").innerHTML = "SELECT";
  const menuList = MENU && MENU.reduce((a, item) => {
    if (a === '') {
      return a + `
<div id="menu-${item.label}" class="menuItem active">
<pre>
${item.label}
<pre>
</div>
<br/>
`;
    } else {
      return a + `
<div id="menu-${item.label}" class="menuItem">
<pre>
${item.label}
<pre>
</div>
<br/>
`;
    }
  }, '');
  const menu = document.getElementById('menu');
  const calc = document.getElementById('calc');
  menu.style.display = 'unset';
  calc.style.display = 'none';
  menu.innerHTML = menuList;
};

const closeMenu = () => {
  PAGE = 'calc';
  MENU_ITEM = 0;
  document.getElementById("left").innerHTML = "Swap";
  document.getElementById("right").innerHTML = "Menu";
  document.getElementById("middle").innerHTML = "ENTER";
  const menu = document.getElementById('menu');
  const calc = document.getElementById('calc');
  menu.style.display = 'none';
  calc.style.display = 'unset';
}

document.addEventListener('keydown', function (e) {
  e.preventDefault();
  var key6 = e.key;
  switch (key6) {
  case 'EndCall':
  case 'End':
    __PS_MV_REG = [];
    return close();
  case 'Backspace':
    if(PAGE === 'menu') {
      return closeMenu();
    }
    var input = document.getElementById('operand');
    var value = input.innerHTML;
    __PS_MV_REG = [];
    if(value === '') {
      if(STACK.length === 0) {
        return close();
      } else {
        STACK = cdr(STACK);
        return renderStackbang();
      }
    } else {
      return input.innerHTML = value.slice(0, -1);
    }
    return;
  case 'Enter':
    if(PAGE === 'menu') {
      const item = MENU[MENU_ITEM];
      item.fn();
      return closeMenu();
    }
    addToStackbang();
    __PS_MV_REG = [];
    return renderStackbang();
  case 'ArrowDown':
    __PS_MV_REG = [];
    if(PAGE === 'menu') {
      const items = document.getElementsByClassName('menuItem');
      items[MENU_ITEM].className = 'menuItem';
      MENU_ITEM++;
      if(MENU_ITEM === items.length) {
        MENU_ITEM = 0;
      }
      items[MENU_ITEM].className = 'menuItem active';
      items[MENU_ITEM].scrollIntoView({
        behavor: 'smooth',
        block: 'center',
        inline: 'nearest'
      });
      return;
    }
    return doOperationbang(function (x, y) {
      return x + y;
    }, 2);
  case 'ArrowUp':
    __PS_MV_REG = [];
    if(PAGE === 'menu') {
      const items = document.getElementsByClassName('menuItem');
      items[MENU_ITEM].className = 'menuItem';
      MENU_ITEM--;
      if(MENU_ITEM === -1) {
        MENU_ITEM = items.length - 1;
      }
      items[MENU_ITEM].className = 'menuItem active';
      items[MENU_ITEM].scrollIntoView({
        behavor: 'smooth',
        block: 'center',
        inline: 'nearest'
      });
      return;
    }
    return doOperationbang(function (x, y) {
      return x - y;
    }, 2);
  case 'ArrowRight':
    __PS_MV_REG = [];
    return doOperationbang(function (x, y) {
      return x * y;
    }, 2);
  case 'ArrowLeft':
    __PS_MV_REG = [];
    return doOperationbang(function (x, y) {
      return x / y;
    }, 2);
  case 'n':
    __PS_MV_REG = [];
    return doOperationbang(function (x) {
      return x * -1;
    }, 1);
  case '*':
    var input = document.getElementById('operand');
    var value = input.innerHTML;
    __PS_MV_REG = [];
    if(value.indexOf('.') === -1) {
      return input.innerHTML = value + '.';
    }
    return;
  case 'Control':
  case 'SoftLeft':
    var input7 = document.getElementById('operand');
    if (input7.innerHTML === '') {
      if (STACK.length >= 2) {
        var fir = car(STACK);
        var sec = cadr(STACK);
        STACK = [sec, fir].concat(cddr(STACK));
        __PS_MV_REG = [];
        return renderStackbang();
      } else {
        STACK = cdr(STACK);
        return renderStackbang();
      };
    } else {
          __PS_MV_REG = [];
      var leftSoftkey = document.getElementById('left');
      leftSoftkey.innerHTML = 'Swap';
      return clearOperandbang();
    };
  case 'Shift':
  case 'SoftRight': {
    if(PAGE === 'menu') {
      return closeMenu();
    }
    return openMenu();
  }
  case 'Call':
    if (STACK.length >= 2) {
      var fir8 = car(STACK);
      STACK = cdr(STACK).concat([fir8]);
      __PS_MV_REG = [];
      return renderStackbang();
    } else {
      __PS_MV_REG = [];
      return null;
    };
  case 'EndCall':
  case 'End':
    __PS_MV_REG = [];
    return close();
  case 'MicrophoneToggle':
    __PS_MV_REG = [];
    return null;
  default:
    var input9 = document.getElementById('operand');
    var leftSoftkey = document.getElementById('left');
    var value10 = input9.innerHTML;
    leftSoftkey.innerHTML = 'Clear';
    __PS_MV_REG = [];
    return value10 ? (input9.innerHTML = value10 + key6) : (input9.innerHTML = +key6);
  };
});

document.addEventListener('DOMContentLoaded', function () {
  var input = document.getElementById('operand');
  var leftSoftkey = document.getElementById('left');
  var enterKey = document.getElementById('middle');
  var rightSoftkey = document.getElementById('right');
  input.innerHTML = '';
  leftSoftkey.innerHTML = 'Swap';
  enterKey.innerHTML = 'ENTER';
  __PS_MV_REG = [];
  return rightSoftkey.innerHTML = 'Menu';
});
